/**
 * 
 */
package hsrt.vis;

import java.util.HashMap;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.content.Intent;
import android.os.Environment;
import android.os.StatFs;
import java.io.File;

import android.telephony.TelephonyManager;
import android.util.Log;


/**
 * @author tlauria
 *
 */
public class StatusDataSphere extends Sphere {
	private Activity activity;
	private HashMap <String, DataSection> dataHolder = new HashMap<String, DataSection>();
		
	/**
	 * 
	 */
	public StatusDataSphere(Activity activity) {
		System.out.println("Klasse: "+dataHolder.getClass().getName());
        this.activity = activity;

        // dataHolder.signalStrength intern in setSignalStrength
        dataHolder.put("signalStrength", new DataSection(R.string.label_signal_strength, 0, 100));
        setSignalStrength();
        int[] blue = {0, 0, 65536};
        dataHolder.put("batteryStrength", new DataSection(R.string.label_battery_strength, 0, blue));
        setBatteryStrength();
        dataHolder.put("fileSystem", getFileSystemStatus());
        dataHolder.put("callStatus", getCallStatus());
        dataCounter = dataHolder.size();
       
    }
	
	protected void prepare() {
	    for (int i = 0; i < SPHERE_LONGITUDES; i++) {
	        float fi = (float) i / (SPHERE_LONGITUDES);
	        // theta is the longitude
	        float theta =
	            (sd.maxLongitude - sd.minLongitude) * (1.0f - fi) + sd.minLongitude;
	        float sinTheta = (float) Math.sin(theta);
	        float cosTheta = (float) Math.cos(theta);
	        
	        for (int j = 0; j < SPHERE_LATITUDES; j++) {
	            float fj = (float) j / (SPHERE_LATITUDES);
	            // phi is the latitude
	            float phi = PI * fj;
	            float sinPhi = (float) Math.sin(phi);
	            float cosPhi = (float) Math.cos(phi);
	            float x = cosTheta * sinPhi;
	            float y = cosPhi;
	            float z = sinTheta * sinPhi;
	            
	            float norm = 1.0f / length(x, y, z);
	            
	            sd.vertices[sd.vidx++] = toFixed(x * sd.radius + sd.centerX);
	            sd.vertices[sd.vidx++] = toFixed(y * sd.radius + sd.centerY);
	            sd.vertices[sd.vidx++] = toFixed(z * sd.radius + sd.centerZ);
	            //Log.e("vertice",""+sd.vidx+"");

	            sd.normals[sd.nidx++] = toFixed(x * norm);
	            sd.normals[sd.nidx++] = toFixed(y * norm);
	            sd.normals[sd.nidx++] = toFixed(z * norm);
	            
	            //if ((j <= SPHERE_LATITUDES * 0.2) || ((j > SPHERE_LATITUDES * 0.3) && (j <= SPHERE_LATITUDES * 0.75))) {
	            //if (j <= SPHERE_LATITUDES * 0.3) {
	
	            //SPHERE_LONGITUDES
	            // Warum geht i nur bis 23 bis einmal um die Kugel rum ist? Longi geht doch bis 55??? 
	            int fooMax = 24; // = SPHERE_LONGITUDES????	            
	            if(i <= fooMax) {
		            DataSection[] data = new DataSection[dataHolder.values().size()];
		            dataHolder.values().toArray(data);
		            
		            // ganzzahliges aufrunden. Dann haben wir keine Lücken in der Kugel, aber im ungünstigetens Fall ein schmales Feld
		            int div = fooMax / data.length;
		            //Log.e("Anteil",""+fooMax / data.length+"");
		            if(fooMax % data.length != div) {
		            	div++;
		            }
		            
		            //System.out.println("Check "+ i + " DIV "+div+" erg "+(i/div));
		            
		            int[] c = data[(i / div)].getColor(j, SPHERE_LATITUDES);
		            
	            	sd.colors[sd.cidx++] = c[0]; //65536; //rot
	            	sd.colors[sd.cidx++] = c[1]; //gr�n
	            	sd.colors[sd.cidx++] = c[2]; //blau#	            	
	            }
	            else {
	            	sd.colors[sd.cidx++] = 0; //rot
	            	sd.colors[sd.cidx++] = 0; //gr�n
	            	sd.colors[sd.cidx++] = 65536; //blau#	            	
	            }
            	//colors[cidx++] = 0; //Alpha
	                
	            /*if (j <= SPHERE_LATITUDES * prozent[0]) {
	            	colors[cidx++] = 65536; //rot
	                colors[cidx++] = 0; //gr�n
	                colors[cidx++] = 0; //blau	                
	                colors[cidx++] = 0; //Alpha 
	                
	            } else if (j <= (SPHERE_LATITUDES * prozent[0]) + (SPHERE_LATITUDES * prozent[1])) {
	            	colors[cidx++] = 65536; //rot
	                colors[cidx++] = 65536; //gr�n
	                colors[cidx++] = 0; //blau	                    
	                colors[cidx++] = 0; //Alpha
	                
	                schrift[sidx++] = toFixed(x);
	                schrift[sidx++] = toFixed(y);
	                schrift[sidx++] = toFixed(z);
	                
	            } else if (j <= (SPHERE_LATITUDES * prozent[0]) + (SPHERE_LATITUDES * prozent[1]) + (SPHERE_LATITUDES * prozent[2])) {
	            	colors[cidx++] = 0; //rot
	                colors[cidx++] = 65536; //gr�n
	                colors[cidx++] = 0; //blau	                    
	                colors[cidx++] = 0; //Alpha
	                
	            } else {
	            	colors[cidx++] = 0; //rot
	                colors[cidx++] = 0; //gr�n
	                colors[cidx++] = 65536; //blau	                    
	                colors[cidx++] = 0; //Alpha
	            }*/
	            
	            // 0 == black, 65536 == white
	            /*colors[cidx++] = (i % 2) * 65536;
	            colors[cidx++] = 0;
	            colors[cidx++] = (j % 2) * 65536;
	            colors[cidx++] = 65536;*/
	        }
	    }
	    
	    for (int i = 0; i < 25 - 1; i++) {
	        for (int j = 0; j < 25 - 1; j++) {
	            int base = i * 25 + j;
	
	            // Ensure both triangles have the same final vertex
	            // since this vertex carries the color for flat
	            // shading
	            sd.indices[sd.iidx++] = (short) (base);
	            sd.indices[sd.iidx++] = (short) (base + 1);
	            sd.indices[sd.iidx++] = (short) (base + SPHERE_LATITUDES + 1);
	
	            sd.indices[sd.iidx++] = (short) (base + SPHERE_LATITUDES);
	            sd.indices[sd.iidx++] = (short) (base);
	            sd.indices[sd.iidx++] = (short) (base + SPHERE_LATITUDES + 1);
	        }
	    }
	}
	
	private void setSignalStrength() {
		final int signalStrengh = 0; 
		TelephonyManager tm  = (TelephonyManager) this.activity.getSystemService(Context.TELEPHONY_SERVICE); 

		final class CallStateListener extends PhoneStateListener
	     {
	    	 public void onSignalStrengthChanged(int asu) {
	    		 System.out.println("SignalStärke gesetzt "+asu);
	    		 dataHolder.put("signalStrength", new DataSection(R.string.label_signal_strength, asu, 25));
	    	 }
	     }

		tm.listen(new CallStateListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTH
				| PhoneStateListener.LISTEN_SERVICE_STATE);
		//return this.signalStrengh;
		System.out.println(signalStrengh);
	}
	
	private void setBatteryStrength() {
		BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
	        public void onReceive(Context context, Intent intent) {
	            String action = intent.getAction();
                System.out.println("Batterie Level gesetzt");
	            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
	                int level = intent.getIntExtra("level", 0);
	                int scale = intent.getIntExtra("scale", 10);
		    		dataHolder.put("batteryStrength", new DataSection(R.string.label_battery_strength, level, scale));
	            }
	        }
	  	};
	  	//mBatteryInfoReceiver.abortBroadcast();
	}
	
	private DataSection getFileSystemStatus() {
		/**
		 * erste DataSection: interner Speicher
		 */
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());

        /**
         * Je näher param 1 an param 2 ist, desto grüner der Wert. 
         */
		DataSection ds = new DataSection(R.string.label_file_stat, stat.getAvailableBlocks(), stat.getBlockCount());
		/**
		 * zweite DataSection: SD Karte, hier Fake Daten, siehe Doku
		 */
		ds.addData(40, 100);
		return ds;
	}

	private DataSection getCallStatus() {
		/**
		 * Da standardmäßig im Emulator das Call Log erstmal leer ist, sind hier auch fake daten. 
		 */
		int[] green = {0, 65536, 0};
		int[] red = {65536, 0, 0};
		int[] blue = {0, 0, 65536};
		DataSection ds = new DataSection(R.string.label_call_stat, 33, green); // angenommene
		ds.addData(11, red); // getätigte
		ds.addData(33, blue); // entgangene
		return ds;
	}
    public int getLabel (int id){
    	return 1; //this.dataHolder.;
    }	
}